<?php namespace App\Http\Traits;

trait AdminMenu
{
    /**
     * 初始化菜单
     */
    protected function initMenu()
    {
        list($menu, $sidebarHtml) = $this->getNavConfig();
        view()->share('menu', $menu);
        view()->share('silderMenu', $sidebarHtml);
    }


    /**
     * @param $url
     * 获取html菜单
     */
    private function getNavConfig()
    {
        $routeName = \Route::currentRouteName();
        $routeName = explode('.', $routeName);
        $one = isset($routeName['0']) ? $routeName[0] : '';
        $two = isset($routeName['1']) ? $routeName[1] : '';

        $nav = config('admin.menu');
        $navHtml = $sidebarHtml = '';

        foreach ($nav as $key => $value) {
            $navHtml .= '<div class="header-nav-inner">';
            $navHtml .= '<a href="' . url($value['url']) . '"';
            if ($one == $value['tag']) {
                $navHtml .= ' class="at"';
                $sidebarHtml .= '<ul class="content-left-menu clearfix">';
                if (!empty($value['child'])) {
                    foreach ($value['child'] as $sidebar) {
                        $tag = explode('.', $sidebar['tag']);
                        $sidebarHtml .= '<li><a href="' . url($sidebar['url']) . '"';
                        if (isset($tag[1]) && ($two == $tag[1])) {
                            $sidebarHtml .= ' class="on"';
                        }
                        $sidebarHtml .= ' >' . $sidebar['name'] . '</a></li>';
                    }
                    $sidebarHtml .= '</ul>';
                }
            }
            $navHtml .= '>' . $value['name'] . '</a>';
            $navHtml .= '</div>';
        }

        return [$navHtml, $sidebarHtml];
    }
}
